package com.sky.utils;

import io.jsonwebtoken.*;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Map;

public class JwtUtils {

    public static String createJWT(String secretKey, long ttl, Map<String, Object> claim) {
        SignatureAlgorithm hs256 = SignatureAlgorithm.HS256;

        long expMillis = System.currentTimeMillis() + ttl;
        Date date = new Date(expMillis);

        JwtBuilder builder = Jwts.builder().setClaims(claim).signWith(hs256, secretKey.getBytes(StandardCharsets.UTF_8)).setExpiration(date);
        return builder.compact();
    }

    public static Claims parseJWT(String secretKey,String token){
        return Jwts.parser().setSigningKey(secretKey.getBytes(StandardCharsets.UTF_8)).parseClaimsJws(token).getBody();
    }
}
